/*
 * Copyright 2007 Seok-Ho, Yang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package kr.javanese.devtools.m2wtp.builder;

import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

/**
 * @author Seok-Ho, Yang (KR)
 *
 */
public class WTPModuleDependencyBuilder extends IncrementalProjectBuilder {

	public static final String BUILDER_ID = "kr.javanese.devtools.m2wtp.wtpDepBuilder";

	class MavenPomDeltaVisitor implements IResourceDeltaVisitor {
		IProgressMonitor monitor;

		public MavenPomDeltaVisitor(IProgressMonitor monitor) {
			this.monitor = monitor;
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
		 */
		public boolean visit(IResourceDelta delta) throws CoreException {
			IResource resource = delta.getResource();
			switch (delta.getKind()) {
			case IResourceDelta.ADDED:
				// handle added resource
				processPom(resource, monitor);
				break;
			case IResourceDelta.REMOVED:
				// handle removed resource
				break;
			case IResourceDelta.CHANGED:
				// handle changed resource
				processPom(resource, monitor);
				break;
			}
			if (resource!=resource.getProject()) return false;
			//return true to continue visiting children.
			return true;
		}
	}

	class MavenPomResourceVisitor implements IResourceVisitor {
		IProgressMonitor monitor;

		public MavenPomResourceVisitor(IProgressMonitor monitor) {
			this.monitor = monitor;
		}

		public boolean visit(IResource resource) {
			processPom(resource, monitor);
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.core.internal.events.InternalBuilder#build(int,
	 *      java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
	 */
	protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
			throws CoreException {
		if (kind == FULL_BUILD) {
			fullBuild(monitor);
		} else {
			IResourceDelta delta = getDelta(getProject());
			if (delta == null) {
				fullBuild(monitor);
			} else {
				incrementalBuild(delta, monitor);
			}
		}
		return null;
	}

	void processPom(IResource resource, IProgressMonitor monitor) {
		if (resource instanceof IFile && resource.getParent()==resource.getProject()
				&& resource.getName().equals("pom.xml")) {
			IFile file = (IFile) resource;
			getProcessor().process(file, monitor);
		}
	}

	protected void fullBuild(final IProgressMonitor monitor)
			throws CoreException {
		try {
			getProject().accept(new MavenPomResourceVisitor(monitor));
		} catch (CoreException e) {
		}
	}

	private PomProcessor getProcessor() {
		return new PomProcessor();
	}

	protected void incrementalBuild(IResourceDelta delta,
			IProgressMonitor monitor) throws CoreException {
		// the visitor does the work.
		delta.accept(new MavenPomDeltaVisitor(monitor));
	}
}
